<?php
class ElemeApiController  extends CommonController
{
    /*
        查询店铺信息
    */
    public function getShop()
    {
        $store_id = $_POST['store_id'];
        $chn_store_id = $_POST['chn_store_id'];

        $ElemeApiModel = new  ElemeApiModel();

        $result = $ElemeApiModel->getShop($store_id, $chn_store_id);

        if($result == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($result['errNo'])){
            $this->_response($result);
        }

        $res = array(
            "errNo" => "0",
            "data" => $result
        );
        
        $this->_response($res);
    }

    /*
        修改店铺信息
    */
    public function updateShop()
    {
        $chn_id = $_POST['chn_id'];
        $store_id = $_POST['store_id'];
        $other_shop_name = $_POST['other_shop_name'];
        $other_shop_desc = $_POST['other_shop_desc'];
        $start_time = $_POST['start_time'];
        $end_time = $_POST['end_time'];
        $shipping_fee = $_POST['shipping_fee'];
        $store_status = $_POST['store_status'];

        $ElemeApiModel = new  ElemeApiModel();

        $result = $ElemeApiModel->updateShop($chn_id, $store_id, $other_shop_name, $other_shop_desc, $start_time, $end_time, $shipping_fee, $store_status);

        if($result == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($result['errNo'])){
            $this->_response($result);
        }

        $res = array(
            "errNo" => "0",
            "data" => $result
        );
        
        $this->_response($res);
    }

    /*
        初始化自定义分类
    */
    public function initCategoryInfo()
    {
        $store_id = $_POST['store_id'];

        $ElemeApiModel = new  ElemeApiModel();

        $initCategoryInfo = $ElemeApiModel->initCategoryInfo($store_id);

        if($initCategoryInfo == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($initCategoryInfo['errNo'])){
            $this->_response($initCategoryInfo);
        }

        $res = array(
            "errNo" => "0",
            "data" => $initCategoryInfo
        );
        
        $this->_response($res);
    }

    /*
        添加商品分类
    */
    public function createCategory()
    {
        $storeId = $_POST['store_id'];
        $category_id = $_POST['category_id'];
        $category_name = $_POST['category_name'];
        $description = $_POST['description'];

        $ElemeApiModel = new  ElemeApiModel();

        $result = $ElemeApiModel->createCategory($storeId, $category_id, $category_name, $description);

        if($result == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($result['errNo'])){
            $this->_response($result);
        }

        $res = array(
            "errNo" => "0",
            "data" => $result
        );
        
        $this->_response($res);
    }

    /*
        修改商品分类
    */
    public function updateCategory()
    {
        $other_category_id = $_POST['other_category_id'];
        $other_category_name = $_POST['other_category_name'];

        $ElemeApiModel = new  ElemeApiModel();

        $result = $ElemeApiModel->updateCategory($other_category_id, $other_category_name);

        if($result == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($result['errNo'])){
            $this->_response($result);
        }

        $res = array(
            "errNo" => "0",
            "data" => $result
        );
        
        $this->_response($res);
    }

    /*
        删除商品分类
    */
    public function removeCategory()
    {
        $store_id = $_POST['store_id'];
        $other_category_id = $_POST['other_category_id'];

        $ElemeApiModel = new  ElemeApiModel();

        $result = $ElemeApiModel->removeCategory($store_id, $other_category_id);

        if($result == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($result['errNo'])){
            $this->_response($result);
        }

        $res = array(
            "errNo" => "0",
            "data" => $result
        );
        
        $this->_response($res);
    }

    /*
        添加商品
    */
    public function createItem()
    {
        $store_id = $_POST['store_id'];
        $product_id = $_POST['product_id']; 
        
        $other_category_id = $_POST['other_category_id'];

        $ElemeApiModel = new  ElemeApiModel();

        $result = $ElemeApiModel->createItem($store_id, $product_id, $other_category_id);

        if($result == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($result['errNo'])){
            $this->_response($result);
        }

        $res = array(
            "errNo" => "0",
            "data" => $result
        );
        
        $this->_response($res);
    }

    /*
        修改商品
    */
    public function updateItem()
    {
        $other_product_id = $_POST['other_product_id']; 
        $other_category_id = $_POST['other_category_id'];
        $other_product_name = $_POST['other_product_name'];

        $ElemeApiModel = new  ElemeApiModel();

        $result = $ElemeApiModel->updateItem($other_product_id, $other_category_id, $other_product_name);

        if($result == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($result['errNo'])){
            $this->_response($result);
        }

        $res = array(
            "errNo" => "0",
            "data" => $result
        );
        
        $this->_response($res);
    }

    /*
        删除商品
    */
    public function removeItem()
    {
        $store_id = $_POST['store_id']; 
        $other_product_id = $_POST['other_product_id']; 

        $ElemeApiModel = new  ElemeApiModel();

        $result = $ElemeApiModel->removeItem($store_id, $other_product_id);

        if($result == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($result['errNo'])){
            $this->_response($result);
        }

        $res = array(
            "errNo" => "0",
            "data" => $result
        );
        
        $this->_response($res);
    }

    /*
        同步商品价格信息
    */
    public function syncPrice()
    {
        $data = array();

        $ElemeApiModel = new ElemeApiModel();

        $resp = $ElemeApiModel->syncPrice($data);

        if($resp == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($resp['errNo'])){
            $this->_response($resp);
        }

        $res = array(
            "errNo" => "0",
            "data" => $resp
        );
        
        $this->_response($res);
    }

}
?>